import {useEffect, useState} from 'react';
import {hostAddr} from '@/config.js';

export function usePronunciationAnalysis(messageId) {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    if (!messageId) return;

    const url = `${hostAddr}/pronunciation/analysis-sync/${messageId}`;

    fetch(url)
    .then(res => res.json())
    .then(json => setData(json))
    .catch(err => setError(err))
    .finally(() => setLoading(false));
  }, [messageId]);

  return {data, loading, error};
}

export default usePronunciationAnalysis;